#include "userCan.h"


#ifdef STM32H7xx_HAL_H

void USER_FDCAN1_INIT(UserCAN_HandleTypeDef *hfdcan)
{
    static uint8_t user_fdcan1_init_number = 0;
    if(user_fdcan1_init_number == 0)
    {
        HAL_FDCAN_ConfigGlobalFilter(hfdcan, FDCAN_ACCEPT_IN_RX_FIFO0, FDCAN_ACCEPT_IN_RX_FIFO0, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE);
        
        HAL_FDCAN_ActivateNotification(hfdcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);

        HAL_FDCAN_Start(hfdcan);
        user_fdcan1_init_number = 1;
    }
}

void USER_FDCAN2_INIT(UserCAN_HandleTypeDef *hfdcan)
{
    static uint8_t user_fdcan2_init_number = 0;
    if(user_fdcan2_init_number == 0)
    {   
        HAL_FDCAN_ConfigGlobalFilter(hfdcan, FDCAN_ACCEPT_IN_RX_FIFO0, FDCAN_ACCEPT_IN_RX_FIFO0, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE);
        
        HAL_FDCAN_ActivateNotification(hfdcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);

        HAL_FDCAN_Start(hfdcan);
        user_fdcan2_init_number = 1;
    }
}

void USER_FDCAN3_INIT(UserCAN_HandleTypeDef *hfdcan)
{
    static uint8_t user_fdcan3_init_number = 0;
    if(user_fdcan3_init_number == 0)
    {
        HAL_FDCAN_ConfigGlobalFilter(hfdcan, FDCAN_ACCEPT_IN_RX_FIFO0, FDCAN_ACCEPT_IN_RX_FIFO0, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE);
        
        HAL_FDCAN_ActivateNotification(hfdcan, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0);

        HAL_FDCAN_Start(hfdcan);
        user_fdcan3_init_number = 1;
    }
}


void user_get_FDCAN_FIFO0_Msgs(UserCAN_HandleTypeDef *hfdcan, user_FDCAN_Rx_Struct *FDCAN_Rx_struct)
{
	HAL_FDCAN_GetRxMessage(hfdcan,FDCAN_RX_FIFO0,&FDCAN_Rx_struct->FDCAN_RxHeader, FDCAN_Rx_struct->Data);
}

void user_get_FDCAN_FIFO1_Msgs(UserCAN_HandleTypeDef *hfdcan, user_FDCAN_Rx_Struct *FDCAN_Rx_struct)
{
	HAL_FDCAN_GetRxMessage(hfdcan,FDCAN_RX_FIFO0,&FDCAN_Rx_struct->FDCAN_RxHeader, FDCAN_Rx_struct->Data);
}



#elif defined(__STM32F4xx_HAL_H) || defined(__STM32F1xx_HAL_H)

    void USER_CAN1_INIT(UserCAN_HandleTypeDef *hcan)
    {
        static uint8_t user_can1_init_number = 0;
        
        CAN_FilterTypeDef sFilterConfig;
        

        if(user_can1_init_number == 0) 
        {
            user_can1_init_number = 1;

            sFilterConfig.FilterBank=1;
            sFilterConfig.FilterIdHigh=0;
            sFilterConfig.FilterIdLow=0;
            sFilterConfig.FilterMaskIdHigh=0;
            sFilterConfig.FilterMaskIdLow=0;
            sFilterConfig.FilterFIFOAssignment=CAN_FILTER_FIFO0;
            sFilterConfig.FilterMode=CAN_FILTERMODE_IDMASK;
            sFilterConfig.FilterScale=CAN_FILTERSCALE_32BIT;
            sFilterConfig.FilterActivation=ENABLE;
            sFilterConfig.SlaveStartFilterBank=14;
            
            if(HAL_CAN_ConfigFilter(hcan,&sFilterConfig)!=HAL_OK)
            {
                Error_Handler();
            }
            
            HAL_CAN_Start(hcan);
            HAL_CAN_ActivateNotification(hcan,CAN_IT_RX_FIFO0_MSG_PENDING);
        }
    }

		#ifdef CAN2
    void USER_CAN2_INIT(UserCAN_HandleTypeDef *hcan)
    {
        static uint8_t user_can2_init_number = 0;
        
        CAN_FilterTypeDef sFilterConfig;
        

        if(user_can2_init_number == 0)
        {
            user_can2_init_number = 1;

            sFilterConfig.FilterBank=14;
            sFilterConfig.FilterIdHigh=0;
            sFilterConfig.FilterIdLow=0;
            sFilterConfig.FilterMaskIdHigh=0;
            sFilterConfig.FilterMaskIdLow=0;
            sFilterConfig.FilterFIFOAssignment=CAN_FILTER_FIFO0;
            sFilterConfig.FilterMode=CAN_FILTERMODE_IDMASK;
            sFilterConfig.FilterScale=CAN_FILTERSCALE_32BIT;
            sFilterConfig.FilterActivation=ENABLE;
            sFilterConfig.SlaveStartFilterBank=14;
            
            if(HAL_CAN_ConfigFilter(hcan,&sFilterConfig)!=HAL_OK)
            {
                Error_Handler();
            }
            
            HAL_CAN_Start(hcan);
            HAL_CAN_ActivateNotification(hcan,CAN_IT_RX_FIFO0_MSG_PENDING);
        }
    }
		#endif

#endif






